using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	using AppManagement.Helpers;
	#endregion
	public class PHIEUNHAPKHORepository
	{
		public static List<PHIEUNHAPKHO> SelectAll()
		{
			try
			{
                return DataContext.Instance.PHIEUNHAPKHO.OrderByDescending(n => n.NGAY_NHAP_KHO).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<PHIEUNHAPKHO>();
		}

		public static PHIEUNHAPKHO SelectByID(int phieu_nhap_kho_id)
		{
			try
			{
				return DataContext.Instance.PHIEUNHAPKHO.FirstOrDefault(item => item.PHIEU_NHAP_KHO_ID == phieu_nhap_kho_id );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public static bool Insert(PHIEUNHAPKHO obj)
		{
			try
			{
				DataContext.Instance.PHIEUNHAPKHO.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Delete(int phieu_nhap_kho_id)
		{
			try
			{
				var delitem = DataContext.Instance.PHIEUNHAPKHO.FirstOrDefault(item => item.PHIEU_NHAP_KHO_ID == phieu_nhap_kho_id );
				DataContext.Instance.PHIEUNHAPKHO.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static List<PHIEUNHAPKHO> RetrieveByID(int phieu_nhap_kho_id)
		{
			try
			{
				return (from item in DataContext.Instance.PHIEUNHAPKHO where  item.PHIEU_NHAP_KHO_ID == phieu_nhap_kho_id  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<PHIEUNHAPKHO>();
		}

		public static List<PHIEUNHAPKHO> SelectByPHIEUDATHANG_ID(int phieudathang_id)
		{
			try
			{
                var lstItem = (from item in DataContext.Instance.PHIEUNHAPKHO where item.PHIEUDATHANG_ID == phieudathang_id select item).OrderByDescending(n => n.NGAY_NHAP_KHO).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<PHIEUNHAPKHO>();
		}

		public static List<PHIEUNHAPKHO> SelectByNHAN_VIEN_ID(int nhan_vien_id)
		{
			try
			{
                var lstItem = (from item in DataContext.Instance.PHIEUNHAPKHO where item.NHAN_VIEN_ID == nhan_vien_id select item).OrderByDescending(n => n.NGAY_NHAP_KHO).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<PHIEUNHAPKHO>();
		}

	}
}